const OpenAI = require('openai');

class MyOpenAI {
  constructor() {
    this.openai = new OpenAI({
      baseURL: 'https://api.deepseek.com',
      apiKey: 'sk-f050ab668f64413e983fd7cff193d17e',
    });
  }

  async getScore(sysAnswer, userAnswer) {
    return new Promise(async (resolve, reject) => {
      try {
        const completion = await this.openai.chat.completions.create({
          messages: [
            { role: 'system', content: '你是全能的判题机器，你将会收到参考答案（title:题目、answer/answer_text:答案、score:题目分值）和用户答案（answer:用户答案），准确的判断，最终以JSON形式{total_score:总得分,list:[2,2,3,4,2,...]}返回用户总得分和每题得分。如果用户答案为空则不给分。' },
            { role: 'user', content: `参考答案：${sysAnswer}，用户答案：${userAnswer}` },
          ],
          model: 'deepseek-chat',
          response_format: { type: 'json_object' },
        });
        resolve(completion.choices[0].message.content);
      } catch (error) {
        reject(error);
      }
    });
  }
}

module.exports = MyOpenAI;
